/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package db;

import common.SimpleDate;
import java.sql.ResultSet;
import java.util.ArrayList;
import javax.print.attribute.standard.DateTimeAtProcessing;
import models.Storage;
import models.StorageItem;
import java.util.Date;
import javax.print.attribute.standard.DateTimeAtCompleted;
import models.Rent;

/**
 *
 * @author Dan
 */
public class RentItemDb extends AbstractDb implements RentItemInterface {

    public boolean saveRentedIteams(Rent rentItem) {
        boolean succeed = false;
        int rc = 0;
        // Pelle har smadret det hele (eller, mangel på samme).
        // Tilføjet foreach-loop for items, og rettet navnet på metoden samt dens argumenter
        try {
            for (StorageItem item : rentItem.getRentItem()) {
                rc = this.db.queryInsert("INSERT INTO Lif_Rental (rental_personid, rental_storageItemid, rental_date, rental_period)  VALUES("
                        + "'" + rentItem.getPersonId() + "',"
                        + "'" + item.getStorageId() + "',"
                        + "'" + rentItem.getRentalDate() + "',"
                        + "'" + rentItem.getRentalPeriod() + "');");
            }
        } catch (Exception e) {
            System.out.println("Der skete en fejl så der blev ikke oprettet noget Item");
        }
        if (rc > -1) {
            succeed = true;
        }
        return succeed;

    }

    public ArrayList<Rent> getRent(int stockId, int personId) {
        ArrayList<Rent> rentList = new ArrayList<Rent>();
        ArrayList<StorageItem> storageItemList = new ArrayList<StorageItem>();

        String qurry = "";
        if (stockId > 0 && personId > 0) {
            qurry = " And storageItem_id =" + stockId + "AND rental_personid =" + personId;
        } else {
            if (stockId > 0) {
                qurry = " And storageItem_id =" + stockId;
            } else if (personId > 0) {
                qurry = " And rental_personid =" + personId;
            }
        }
        try {
            ResultSet Item = this.db.query("SELECT * FROM Lif_Storage, Lif_StorageItem right JOIN Lif_Rental ON storageItem_id = rental_storageItemid WHERE storageItem_storageid = storage_id" + qurry);
            if (Item != null) {
                while (Item.next()) {
                    Storage storageObj = new Storage(Item.getString("storage_name"), Item.getString("storage_description"), Item.getInt("storage_amount"));
                    StorageItem storageItemObj = new StorageItem(storageObj, Item.getInt("storageItem_storageId"), Item.getInt("storageItem_amount"), Item.getInt("storageItem_lostAmount"), Item.getString("storageitem_serialnumber"));
                    storageItemList.add(storageItemObj);

                    Rent rentItem = new Rent(
                            Item.getInt("rental_personid"),
                            Item.getString("rental_date"),
                            Item.getInt("rental_period"),
                            storageItemList);
                    rentList.add(rentItem);
                }
            }
        } catch (Exception e) {
        }
        return rentList;
    }
}
